package com.xnyzc.lhy.order.controller.dapi.order;

import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.DateUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.order.entity.param.cms.PageDto;
import com.xnyzc.lhy.order.entity.param.order.CharteredOrderInfoParam;
import com.xnyzc.lhy.order.entity.param.order.OrderInfoParam;
import com.xnyzc.lhy.order.service.impl.order.OaDOrderServiceImpl;
import com.xnyzc.lhy.order.service.order.IOaDOrderCorrelationService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 司机订单相关控制器
 *
 * @Author: huxuekuo
 * @Date: 2019-07-22 14:11
 * @Description:
 */
@RestController
@RequestMapping("/oa/dapi/orderCorrelation")
public class DOrderCorrelationController {

    @Autowired
    private IOaDOrderCorrelationService iOaDOrderService;
    @Autowired
    private OaDOrderServiceImpl oaDOrderService;
    /**
     * 司机端订单列表
     *
     * @param type 订单类型
     * @param page 页码
     * @return
     */
    @ApiOperation(value = "司机端订单列表", notes = "司机端订单列表")
    @GetMapping(value = "/dOrderList")
    public Rv dOrderList(
            @ApiParam(name = "type", value = "订单类型") String type,
            @ApiParam(name = "page", value = "页码") Integer page) {
        Long userId = TokenUtil.getTokenUserId();
        return iOaDOrderService.dOrderList(userId, type, page);
    }

    /**
     * 司机端订单详情
     *
     * @param orderNo 订单号
     * @return
     */
    @ApiOperation(value = "司机端订单详情", notes = "司机端订单详情")
    @GetMapping(value = "/dOrderDetails")
    public Rv dOrderList(
            @ApiParam(name = "orderNo", value = "订单号") String orderNo) {

        String userType = TokenUtil.getUserType();
        String tag = TokenUtil.getTag();
        return iOaDOrderService.dOrderDetails(orderNo, userType, tag);
    }

    /**
     * 司机端行程列表
     *
     * @return
     */
    @ApiOperation(value = "司机端行程列表", notes = "司机端行程列表")
    @PostMapping(value = "/getDriverOrder")
    public Rv getDriverOrder(@RequestBody PageDto pageDto) {
        Long driverId = TokenUtil.getTokenUserId();
        return iOaDOrderService.getDriverOrder(driverId, pageDto);
    }

    /**
     * 司机端改派订单请求
     *
     * @return
     */
    @ApiOperation(value = "司机端改派订单请求", notes = "司机端改派订单请求")
    @PostMapping(value = "/getDriverReassignmentOrder")
    public Rv getDriverReassignmentOrder(@ApiParam(name = "orderNo", value = "订单号") String orderNo) {
        Long driverId = TokenUtil.getTokenUserId();
        return iOaDOrderService.getDriverReassignmentOrder(driverId, orderNo);
    }

    @ApiOperation(value = "司机端预约行程列表", notes = "司机端预约行程列表")
    @PostMapping(value = "/getDriverBookingOrder")
    public Rv getDriverBookingOrder(@RequestBody PageDto pageDto) {
        Long driverId = TokenUtil.getTokenUserId();
        return iOaDOrderService.getDriverBookingOrder(driverId, pageDto);
    }

    /**
     * 司机端行程详情
     */
    @ApiOperation(value = "司机端行程详情", notes = "司机端行程详情")
    @PostMapping(value = "/getOrderInfo")
    public Rv getOrderInfo(@RequestBody OrderInfoParam orderInfoParam) throws ParseException {
        if (CheckUtil.objIsEmpty(orderInfoParam)) {
            throw PangException.create(EErrorCode.missingArg);
        }
        Long appointmentTime = orderInfoParam.getAppointmentTime();
        Date apptime=null;
        if (null!=appointmentTime){
            apptime=new Date(appointmentTime);
        }
        return iOaDOrderService.getOrderInfo(orderInfoParam.getOrderNo(),apptime);
    }

    /**
     * 包车开始行程
     */
    @ApiOperation(value = "包车开始行程", notes = "包车开始行程")
    @PostMapping(value = "/charteredStart")
    public Rv charteredStart(@RequestBody CharteredOrderInfoParam charteredOrderInfoParam) {
        if (CheckUtil.objIsEmpty(charteredOrderInfoParam)) {
            throw PangException.create(EErrorCode.missingArg);
        }
        Long driverId = TokenUtil.getTokenUserId();
        if (driverId == null) {
            throw PangException.create(EErrorCode.tokenExpire);
        }
        return iOaDOrderService.charteredStart(charteredOrderInfoParam,driverId);
    }

    /**
     * 包车结束行程
     */
    @ApiOperation(value = "包车结束行程", notes = "包车结束行程")
    @PostMapping(value = "/charteredEnd")
    public Rv charteredEnd(@RequestBody CharteredOrderInfoParam charteredOrderInfoParam) {
        if (CheckUtil.objIsEmpty(charteredOrderInfoParam)) {
            throw PangException.create(EErrorCode.missingArg);
        }
        Long driverId = TokenUtil.getTokenUserId();
        if (driverId == null) {
            throw PangException.create(EErrorCode.tokenExpire);
        }
        return iOaDOrderService.charteredEnd(charteredOrderInfoParam,driverId);
    }
    /**
     * 包车结束订单
     */
    @ApiOperation(value = "包车结束整个订单",notes = "包车结束整个订单")
    @PostMapping(value = "/allCharteredEnd")
    public Rv allCharteredEnd(@ApiParam(name = "orderNo",value = "订单编号")
                              @RequestParam(value = "orderNo") String orderNo){
        if (CheckUtil.strIsEmpty(orderNo)){
            throw PangException.create(EErrorCode.missingArg);
        }
        return iOaDOrderService.allCharteredEnd(orderNo);
    }

    @ApiOperation(value = "app同步时间", notes = "app同步时间")
    @PostMapping(value = "/syncTime")
    public Rv syncTime() {
        long l = System.currentTimeMillis();
        return Rv.wrap(0,"",l);
    }

    /**
     * 费用明细查询
     */
    @ApiOperation(value = "费用明细查询", notes = "费用明细查询")
    @PostMapping(value = "/getBillDetails")
    public Rv getBillDetails(
            @RequestBody OrderInfoParam orderInfoParam, HttpServletRequest request
    ) {
        if (CheckUtil.objIsEmpty(orderInfoParam)) {
            throw PangException.create(EErrorCode.missingArg);
        }
        String appCode = request.getHeader("appCode");
        return iOaDOrderService.getBillDetails(orderInfoParam.getOrderNo(), appCode);

    }
    @PostMapping(value = "/sendBill")
    public Rv sendBill(@RequestParam("driverId") Long driverId, @RequestParam("orderId") String orderId, @RequestParam("drawFee") BigDecimal drawFee){
        oaDOrderService.saveOaDBill(driverId,orderId,drawFee);
        return Rv.wrap();
    }

    // ------------------- 首页 ##

    /**
     * 首页接口 (不用)
     */
    @ApiOperation(value = "首页", notes = "首页")
    @PostMapping(value = "/Home")
    public Rv Home() {
        return iOaDOrderService.getHomeDetails();
    }



}
